<template>
    <el-dialog
      title="修改企业信息"
      :visible.sync="localVisible"
      width="550px"
      custom-class="custom-dialog"
      :before-close="handleClose">
      <el-form
        ref="updateFormRef"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm">
        <!-- 企业名称 -->
        <el-form-item label="企业名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入企业名称"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
        <!-- 企业地址 -->
        <el-form-item label="企业地址" prop="address">
          <el-input
            v-model="form.address"
            placeholder="请输入企业地址"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
        <!-- 企业电话 -->
        <el-form-item label="企业电话" prop="enterprisePhone">
          <el-input
            v-model="form.enterprisePhone"
            placeholder="请输入企业电话"
            clearable
            style="width: 200px;"></el-input>
        </el-form-item>
        <!-- 负责联系人 -->
        <el-form-item label="负责联系人" prop="contact">
          <el-input
            v-model="form.contact"
            placeholder="请输入负责联系人"
            clearable
            style="width: 200px;"></el-input>
        </el-form-item>
        <!-- 联系人电话 -->
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input
            v-model="form.contactPhone"
            placeholder="请输入联系人电话"
            clearable
            style="width: 200px;"></el-input>
        </el-form-item>
        <!-- 企业邮箱 -->
        <el-form-item label="企业邮箱" prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入企业邮箱"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
        <!-- 企业官网 -->
        <el-form-item label="企业官网" prop="website">
          <el-input
            v-model="form.website"
            placeholder="请输入企业官网"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
        <!-- 企业简介 -->
        <el-form-item label="企业简介" prop="introduction">
          <el-input
            v-model="form.introduction"
            type="textarea"
            :rows="4"
            placeholder="请输入企业简介"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
        <!-- 企业Logo -->
        <el-form-item label="企业Logo" prop="logo">
          <el-input
            v-model="form.logo"
            placeholder="请输入企业Logo链接"
            clearable
            style="width: 300px;"></el-input>
        </el-form-item>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" class="cancel-btn">取消</el-button>
        <el-button type="primary" @click="handleSubmit" class="submit-btn">保存</el-button>
      </div>
    </el-dialog>
  </template>


<script>
import { update } from '@/api/enterprise'; // 假设有一个更新接口

export default {
  name: "UpdateEnterpriseDialog",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    enterprise: {
      type: Object,
      default: () => ({}), // 默认为空对象
    },
  },
  data() {
    return {
      localVisible: this.visible, // 本地变量控制对话框显示状态
      form: {
        id: null, // 企业ID
        name: "",
        address: "",
        enterprisePhone: "",
        contact: "",
        contactPhone: "",
        email: "",
        website: "",
        introduction: "",
        logo: "",
        userId: this.$store.state.user.id
      },
      rules: {
        name: [{ required: true, message: "企业名称不能为空", trigger: "blur" }],
        contact: [{ required: true, message: "负责联系人不能为空", trigger: "blur" }],
        contactPhone: [{ required: true, message: "联系人电话不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {
    // 监听父组件传递的 visible 变化，并同步到本地变量
    visible(newVal) {
      this.localVisible = newVal;
    },
    // 监听父组件传递的企业对象变化，并更新表单数据
    enterprise: {
      immediate: true,
      handler(newVal) {
        if (newVal && Object.keys(newVal).length > 0) {
          this.form = { ...this.form, ...newVal }; // 合并数据
        }
      },
    },
  },
  methods: {
    // 关闭对话框
    handleClose(done) {
      this.$emit("update:visible", false); // 通知父组件关闭对话框
      if (done) done();
    },
    // 点击取消按钮
    handleCancel() {
      this.handleClose(); // 关闭对话框
    },
    // 点击保存按钮
    handleSubmit() {
      this.$refs.updateFormRef.validate((valid) => {
        if (valid) {
          update(this.form)
            .then((response) => {
              if (response.code === 200) {
                this.$message({
                  message: "修改企业成功！",
                  type: "success",
                });
                this.resetForm(); // 清空表单
                this.handleClose(); // 关闭对话框
                this.$emit("on-success"); // 通知父组件刷新列表
              } else {
                this.$message({
                  message: "修改企业失败，请稍后再试！",
                  type: "error",
                });
              }
            })
            .catch((error) => {
              console.error("修改企业失败:", error);
              this.$message({
                message: "网络错误，请检查您的网络连接！",
                type: "error",
              });
            });
        } else {
          this.$message({
            message: "请填写完整信息！",
            type: "warning",
          });
          return false;
        }
      });
    },
    // 重置表单
    resetForm() {
      this.$refs.updateFormRef.resetFields();
    },
  },
};
</script>


<style scoped>
/* 对话框整体样式 */
.custom-dialog {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 标题样式 */
.custom-dialog .el-dialog__header {
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
}

.custom-dialog .el-dialog__title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
}

/* 表单样式 */
.demo-ruleForm {
  padding: 20px;
}

.demo-ruleForm .el-form-item__label {
  font-weight: normal;
  color: #606266;
}

/* 输入框样式 */
.demo-ruleForm .el-input__inner {
  border-radius: 4px;
  transition: border-color 0.3s ease;
}

.demo-ruleForm .el-input__inner:focus {
  border-color: #409eff;
  box-shadow: 0 0 4px rgba(64, 158, 255, 0.5);
}

/* 按钮样式 */
.dialog-footer {
  text-align: right;
  padding: 20px;
  border-top: 1px solid #ebeef5;
}

.cancel-btn {
  background-color: #f5f7fa;
  border: 1px solid #dcdfe6;
  color: #606266;
  transition: all 0.3s ease;
}

.cancel-btn:hover {
  background-color: #ebedf0;
  border-color: #c0c4cc;
}

.submit-btn {
  background-color: #409eff;
  border: 1px solid #409eff;
  color: #fff;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}
</style>